package com.cjg.xiancheng;

import java.util.concurrent.locks.ReentrantLock;

public class Lock {
    public static void main(String[] args) {
        ticket ticket = new ticket();
        new Thread(ticket).start();   new Thread(ticket).start();   new Thread(ticket).start();
    }
}
class ticket implements Runnable{
    private int i=10;
    private final ReentrantLock lock =new ReentrantLock();

    @Override
    public void run() {
        try {
            buy1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void buy1() throws InterruptedException {

        try {
            lock.lock();
            while(true){
                if (i<=0){
                    return;
                }
                //时延用来放大问题
                Thread.sleep(1000);
                System.out.println(i--);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
